﻿// 页面载入时执行
$(function () {
	//==== 鼠标移到表上时，高亮显示一行
	$('table.list tr').hover(function () {
		$(this).addClass('listhover');
	},
	function () {
		$(this).removeClass('listhover');
	});
});

// 选择全部
function chkall(obj) {
	$("input:checkbox[name='chk']").attr('checked', obj.checked);
}

// 获取全部选择项id, 用逗号分割，存入$("#sel")中，如果有选项，返回true, 否则返回false
function getcheckedstrid() {
	var $objs = $("input:checkbox[name='chk']:checked");
	var n = $objs.size();
	if (n === 0)
	{
		alert('请选择列表项。');
		return false;
	}

	var sel = $objs.eq(0).val();	// 第一项
	for (var i = 1; i < n; i++)		// i=1, 从第二项开始加
		sel += "," + $objs.eq(i).val(); // 中间用逗号分隔

	$("#sel").val(sel);
	return true;
}

// 返回第一个选择项id, 如果没有选项，返回null
function getfirstchecked() {
	$obj = $("input:checkbox[name='chk']:checked");
	if ($obj[0])
		return $obj.eq(0).val();
	else{
		alert('请选择列表项。');
		return null;
	}
}

// 页码控件， 输入参数data，JSON数据：
// ctrlid jquery对象字符串，如"#pager"
// page 整数，当前页，从1开始
// pages 页数
// pagenu 可选，显示页码的数量，最小5，默认显示15个页码。
function HPages(data) {
	if (!data['ctrlid'] || !data['page'] || !data['pages']) {
		return;
	}

	var total = data['pages'];
	if (total <= 0)
		return;

	var current = data['page'];
	//	不验证，由上级程序保证
	//if (current < 0)
	//	current = 1;
	//else if (current > total)
	//	current = total;

	current--; //以下从0开始
	var $o = $(data['ctrlid']);
	var num = data['pagenu'] ? data['pagenu'] : 15;

	var htmstr = "";
	if (total <= num) {
		for (var i = 0; i < total; ) {
			if (i == current)
				htmstr += "<a class='current'>" + ++i + "</a> ";
			else
				htmstr += "<a>" + ++i + "</a> ";
		}
	}
	else if (current < num / 2) {
		for (i = 0; i < num - 1; ) {
			if (i == current)
				htmstr += "<a class='current'>" + ++i + "</a> ";
			else
				htmstr += "<a>" + ++i + "</a> ";
		}
		htmstr += "... <a>" + total + "</a>";
	}
	else if (total - current - 1 < num / 2) {
		htmstr += "<a>" + 1 + "</a> ... ";
		for (i = total - num + 1; i < total; ) {
			if (i == current)
				htmstr += "<a class='current'>" + ++i + "</a> ";
			else
				htmstr += "<a>" + ++i + "</a> ";
		}
	}
	else {
		htmstr += "<a>" + 1 + "</a> ... ";
		for (i = current - parseInt(num / 2) + 1, j = 0; j < num - 2; j++) {
			if (i == current)
				htmstr += "<a class='current'>" + ++i + "</a> ";
			else
				htmstr += "<a>" + ++i + "</a> ";
		}
		htmstr += "... <a>" + total + "</a>";
	}

	htmstr += " &nbsp;第<input id='pageedit' type='text'/>页 <a id='pagego'>GO</a>"

	$o.html(htmstr);

	$o.children("a").click(function () {
		var pa = $(this).text();
		if (pa === 'GO') {
			pa = $o.children("#pageedit").val()
			if (!pa) {
				alert("请输入页码");
				return;
			}
		}
		if (data['gopage'])
			data['gopage'](pa);
	});
}
